<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/6/21.
 */

namespace App\Http\Request\System;

use App\Enum\FormTypeEnum;
use App\Models\ChannelField;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;

class ChannelFieldRequest extends FormRequest
{
    public function rules(): array
    {
        $id = (int) request()->route('id');

        return [
            'field' => [
                'required',
                'max:30',
                'alpha_dash',
                Rule::unique(ChannelField::class, 'field')->ignore($id),
                function ($attribute, $value, $fail): void {
                    $value = Str::upper($value);
                    if (\in_array($value, Config::get('data_keywords'), true)) {
                        $fail(__('validation.retain_keywords', ['attribute' => __('validation.attributes.table_name'), 'value' => $value]));
                    }
                },
            ],
            'title' => 'required|max:50',
            'type' => [
                'required',
                Rule::in(array_values(array_flip(FormTypeEnum::getMaps()))),
            ],
            'tips' => 'max:200',
            'default_val' => 'max:200',
            'is_release' => 'integer|in:0,1',
            'is_search' => 'integer|in:0,1',
            'is_table' => 'integer|in:0,1',
            'is_required' => 'integer|in:0,1',
            'status' => 'integer|in:0,1',
        ];
    }
}
